/* gm_server.c -- Servidor que recebe as informações da Visão
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part os GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 3 of the
 * License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifndef GM_SERVER_H_
#define GM_SERVER_H_

#include <netinet/in.h>

#include "gm_protocol.h"
#include "gm_monitor.h"
#include "gm_control.h"

/* defines */
/** Porta padrão para o servidor */
#define GM_SERVER_DEFAULT_PORT 12345
#define GM_SERVER_DEFAULT_PORT_STR "12345"
/** Tipo padrão de servidor (virtual) */
#define GM_SERVER_DEFAULT_REAL 0
/** O usuário quer ou não utilizar o display do servidor */
#define GM_SERVER_DEFAULT_F_DISPLAY 0

/* Enum */
enum world_pos
{
    WORLD_CORNER_1_PX,
    WORLD_CORNER_1_PY,
    WORLD_CORNER_2_PX,
    WORLD_CORNER_2_PY,
    WORLD_CORNER_3_PX,
    WORLD_CORNER_3_PY,
    WORLD_CORNER_4_PX,
    WORLD_CORNER_4_PY,

    FIELD_CORNER_1_PX,
    FIELD_CORNER_1_PY,
    FIELD_CORNER_2_PX,
    FIELD_CORNER_2_PY,
    FIELD_CORNER_3_PX,
    FIELD_CORNER_3_PY,
    FIELD_CORNER_4_PX,
    FIELD_CORNER_4_PY,

    MIDDLE_FIELD_UP_PX,
    MIDDLE_FIELD_UP_PY,
    MIDDLE_FIELD_DOWN_PX,
    MIDDLE_FIELD_DOWN_PY,

    GOAL_1_PX,
    GOAL_1_PY,
    GOAL_2_PX,
    GOAL_2_PY
};

enum robot_number
{
    ROBOT0,
    ROBOT1,
    ROBOT2,
    ROBOT3,
    ROBOT4
};

typedef struct gm_server_t gm_server_t;
typedef struct gm_server_robot_t gm_server_robot_t;

/** Objeto que comunica-se com a Visão através de sockets e com os
 * gm_robot_t.
 *
 * Na prática ele é um servidor que fica a espera de mensagens da
 * Visão e atualiza as posições no vetor world e field. Também espera
 * conexões dos robôs e atualiza seus vetores.
 */
struct gm_server_t
{
    /* Servidor */
    int socketfd;
    fd_set active_fd_set;
    fd_set read_fd_set;
    pthread_t server_thread;

    int f_real_world;

    gm_monitor_t * monitor;

    gm_control_t * control;
};

gm_server_t * gm_server_new( uint16_t port );
void gm_server_destroy( gm_server_t *self );
void gm_server_init( gm_server_t * self );
void gm_server_add_monitor( gm_server_t * self, gm_monitor_t * monitor );
void gm_server_add_control( gm_server_t * self, gm_control_t * control );

#endif	/* GM_SERVER_H_ */
